﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;
using System.Collections;

using EntitySpaces.Interfaces;

public partial class Admin_SAPFin_SAPFin002 : AdminBasePage
{
    protected string show = "hide";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            drpLocation.Items.Insert(1, new ListItem("未分配存储", "未分配存储"));   
            
            txtStartDate.Value = DateTime.Now.AddMonths(-1).Date.ToString(ConstantValue.DateFormat);
            txtEndate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);
            drpCreator.SelectedValue = LoginUserInfo.CurrentUser.LoginName;

            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["txtPostingStartDate"] != null)
            {
                txtPostingStartDate.Value = Session["txtPostingStartDate"].ToString();
            }
            if (Session["txtPostingEndDate"] != null)
            {
                txtPostingEndDate.Value = Session["txtPostingEndDate"].ToString();
            }
            //if (Session["drpYearStart"] != null)
            //{
            //    drpYearStart.SelectedValue = Session["drpYearStart"].ToString();
            //}
            //if (Session["drpMonthStart"] != null)
            //{
            //    drpMonthStart.SelectedValue = Session["drpMonthStart"].ToString();
            //}
            //if (Session["drpYearEnd"] != null)
            //{
            //    drpYearEnd.SelectedValue = Session["drpYearEnd"].ToString();
            //}
            //if (Session["drpMonthEnd"] != null)
            //{
            //    drpMonthEnd.SelectedValue = Session["drpMonthEnd"].ToString();
            //}
            if (Session["txtBoxNoStart"] != null)
            {
                txtBoxNoStart.Text = Session["txtBoxNoStart"].ToString();
            }
            if (Session["txtBoxNoEnd"] != null)
            {
                txtBoxNoEnd.Text = Session["txtBoxNoEnd"].ToString();
            }
            if (Session["drpDocType"] != null)
            {
                drpDocType.SelectedValue = Session["drpDocType"].ToString();
            }
            if (Session["drpCompany"] != null)
            {
                drpCompany.SelectedValue = Session["drpCompany"].ToString();
            }
            if (Session["drpFunctionDept"] != null)
            {
                drpFunctionDept.SelectedValue = Session["drpFunctionDept"].ToString();
            }
            if (Session["drpLocation"] != null)
            {
                drpLocation.SelectedValue = Session["drpLocation"].ToString();
            }
            if (Session["drpCabin"] != null)
            {
                drpCabin.SelectedValue = Session["drpCabin"].ToString();
            }
            if (Session["drpNumber"] != null)
            {
                drpNumber.SelectedValue = Session["drpNumber"].ToString();
            }
            if (Session["drpCreator"] != null)
            {
                drpCreator.SelectedValue = Session["drpCreator"].ToString();
            }
            if (Session["drpStoreStatus"] != null)
            {
                drpStoreStatus.SelectedValue = Session["drpStoreStatus"].ToString();
            }
            if (Session["drpIsDeleted"] != null) {
                drpIsDeleted.SelectedValue = Session["drpIsDeleted"].ToString();
            }
            if (Session["drpIsUpdate"] != null)
            {
                drpIsUpdate.SelectedValue = Session["drpIsUpdate"].ToString();
            }
        }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["txtPostingStartDate"] = txtPostingStartDate.Value;
        Session["txtPostingEndDate"] = txtPostingEndDate.Value;
        //Session["drpYearStart"] = drpYearStart.SelectedValue;
        //Session["drpMonthStart"] = drpMonthStart.SelectedValue;
        //Session["drpYearEnd"] = drpYearEnd.SelectedValue;
        //Session["drpMonthEnd"] = drpMonthEnd.SelectedValue;
        Session["txtBoxNoStart"] = txtBoxNoStart.Text.Trim();
        Session["txtBoxNoEnd"] = txtBoxNoEnd.Text.Trim();
        Session["drpStoreStatus"] = drpStoreStatus.SelectedValue;

        Session["drpDocType"] = drpDocType.SelectedValue;
        Session["drpCompany"] = drpCompany.SelectedValue;
        Session["drpFunctionDept"] = drpFunctionDept.SelectedValue;  
        Session["drpLocation"] = drpLocation.SelectedValue;
        Session["drpCabin"] = drpCabin.SelectedValue;
        Session["drpNumber"] = drpNumber.SelectedValue;
        Session["drpCreator"] = drpCreator.SelectedValue;
        Session["drpIsDeleted"] = drpIsDeleted.SelectedValue;
        Session["drpIsUpdate"] = drpIsUpdate.SelectedValue;

        Response.Redirect("SAPFin002.aspx");
    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinBoxQuery q1 = new FinBoxQuery("q1");
        BFThirdUnitQuery q2 = new BFThirdUnitQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q5 = new TSCodeTableQuery("q5");
        TSUsersQuery q6 = new TSUsersQuery("q6");
        //BFCompanyDicQuery q6 = new BFCompanyDicQuery("q6");

        ArrayList args = new ArrayList();
        args.Add(q1.CreatedDttm >= txtStartDate.Value);
        args.Add(q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        args.Add(q1.Or(q1.StorageStatus == "S0", q1.StorageStatus == "S88"));//只能查询新建的凭证盒和待入库



        if (!string.IsNullOrEmpty(txtPostingStartDate.Value))
        {
            args.Add(q1.BoxYear >= txtPostingStartDate.Value.Substring(0,4));
            args.Add(q1.BoxMonth >= txtPostingStartDate.Value.Substring(5, 2));
        }

        if (!string.IsNullOrEmpty(txtPostingEndDate.Value))
        {
            args.Add(q1.BoxYear <= txtPostingEndDate.Value.Substring(0, 4));
            args.Add(q1.BoxMonth <= txtPostingEndDate.Value.Substring(5, 2));
        }
        if (!string.IsNullOrEmpty(txtBoxNoStart.Text.Trim()))
        {
            args.Add(q1.BoxNo<=txtBoxNoStart.Text.Trim());
        }
        if (!string.IsNullOrEmpty(txtBoxNoEnd.Text.Trim()))
        {
            args.Add(q1.BoxNo >= txtBoxNoEnd.Text.Trim());
        }
        if (!string.IsNullOrEmpty(drpDocType.SelectedValue) && drpDocType.SelectedValue!="N/A")
        {
            args.Add(q1.DocumentType == drpDocType.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpStoreStatus.SelectedValue))
        {
            args.Add(q1.StorageStatus == drpStoreStatus.SelectedValue);
        }

        if (!string.IsNullOrEmpty(drpCompany.SelectedValue) && drpCompany.SelectedValue != "N/A")
        {
            args.Add(q1.CompanyName == drpCompany.SelectedValue.Replace(")", "").Split('(')[0]);
            args.Add(q1.CompanyCode == drpCompany.SelectedValue.Replace(")", "").Split('(')[1]);
        }
        if (!string.IsNullOrEmpty(drpFunctionDept.SelectedValue))
        {
            args.Add(q1.FunctionDept == drpFunctionDept.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpLocation.SelectedValue))
        {
            args.Add(q1.BoxLocation == drpLocation.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpCabin.SelectedValue))
        {
            args.Add(q1.ThirdUnitCabinId == drpCabin.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpNumber.SelectedValue))
        {
            args.Add(q1.ThirdUnitNumberId == drpNumber.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpCreator.SelectedValue))
        {
            args.Add(q1.CreatedBy == drpCreator.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpIsDeleted.SelectedValue))
        {
            args.Add(q1.IsDeleted == (drpIsDeleted.SelectedValue == "1"));
        }
        if (!string.IsNullOrEmpty(drpIsUpdate.SelectedValue))
        {
            args.Add(q1.IsHis == drpIsUpdate.SelectedValue);
        }    
        q1.LeftJoin(q2).On(q1.BoxLocation==q2.ThirdUnitID);
        q1.InnerJoin(q3).On(q1.DocumentType==q3.Code);
        q1.InnerJoin(q4).On(q1.FunctionDept == q4.Code);
        q1.InnerJoin(q5).On(q1.StorageStatus == q5.Code);
        q1.LeftJoin(q6).On(q2.ManageUserId == q6.UserID);


        q1.Select(q6.LoginName, q1.NeedStoreYear, q1.CompanyCode,q1.CompanyName,q1.IndexStart,q1.IndexEnd, q1.BoxID, (q2.Name +"-"+ q2.CartName).As("LocationName"), q3.CodeDesc.As("DocumentTypeName"), q4.CodeDesc.As("FunctionDeptName")
            , q5.CodeDesc.As("StorageStatusName"),q1.CreatedBy,q1.UpdatedBy,q1.CreatedDttm,q1.UpdatedDttm,q1.CompanyName,q1.BoxYear,q1.BoxMonth,q1.BoxNo,q1.BoxMonthEnd)
            .Where(args.ToArray())
            .OrderBy(q1.CreatedDttm.Descending);




        
        //VBoxReceiptsCollection receipts = new VBoxReceiptsCollection();

        //receipts.Query.Where(receipts.Query.CreatedDttm >= txtStartDate.Value && receipts.Query.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));

        //if (!LoginUserInfo.CurrentUser.IsAdmin)
        //{
        //    receipts.Query.Where(receipts.Query.BoxLocation == LoginUserInfo.CurrentUser.ThirdUnitID);
        //}
        //receipts.Query.OrderBy(receipts.Query.CreatedDttm.Descending);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("txtPostingStartDate");
        Session.Remove("txtPostingEndDate");
        Session.Remove("txtBoxNoStart");
        Session.Remove("txtBoxNoEnd");
        Session.Remove("drpStoreStatus");
        Session.Remove("drpDocType");
        Session.Remove("drpCompany");
        Session.Remove("drpFunctionDept");
        Session.Remove("drpLocation");
        Session.Remove("drpCabin");
        Session.Remove("drpNumber");
        Session.Remove("drpCreator");

    }

    //protected string GetActionUrl(string status, object rowView, int receiptCount)
    //{
    //    string retUrl = "";

    //    if (status.Equals(ConstantValue.StorageStatusCode.InStock))
    //    {
    //        retUrl = GetLink("添加凭证", "A", "Fin002c.aspx", rowView, 980, 580, false, VBoxReceiptsMetadata.ColumnNames.BoxID);
    //        retUrl += "   " + GetUpdateLink("Fin002a.aspx", rowView, 680, 580, VBoxReceiptsMetadata.ColumnNames.BoxID);
    //        if (receiptCount > 0)
    //        {
    //            retUrl += "   " + GetLink("标签打印", "A", "Fin002d.aspx", rowView, 1200, 740, false, VBoxReceiptsMetadata.ColumnNames.BoxID);
    //        }

    //    }

    //    return retUrl;
    //}

    protected void drpLocation_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(drpLocation.SelectedValue))
        {
            BFThirdUnitCabinCollection bFThirdUnitCabinCollection = new BFThirdUnitCabinCollection();
            bFThirdUnitCabinCollection.Query.Where(bFThirdUnitCabinCollection.Query.ThirdUnitID == decimal.Parse(drpLocation.SelectedValue))
                .OrderBy(bFThirdUnitCabinCollection.Query.Name.Ascending);
            bFThirdUnitCabinCollection.Query.Load();
            drpCabin.DataSource = bFThirdUnitCabinCollection;
            drpCabin.DataTextField = BFThirdUnitCabinMetadata.ColumnNames.Name;
            drpCabin.DataValueField = BFThirdUnitCabinMetadata.ColumnNames.ThirdUnitCabinID;
            drpCabin.DataBind();

            drpCabin.Items.Insert(0, new ListItem("请选择凭证仓库号", ""));
            show = "show";
        }
    }

    protected void drpCabin_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(drpCabin.SelectedValue))
        {
            BFThirdUnitNumberCollection bFThirdUnitNumberCollection = new BFThirdUnitNumberCollection();
            bFThirdUnitNumberCollection.Query.Where(bFThirdUnitNumberCollection.Query.ThirdUnitCabinID == Guid.Parse(drpCabin.SelectedValue))
                .OrderBy(bFThirdUnitNumberCollection.Query.Name.Ascending);
            bFThirdUnitNumberCollection.Query.Load();
            drpNumber.DataSource = bFThirdUnitNumberCollection;
            drpNumber.DataTextField = BFThirdUnitNumberMetadata.ColumnNames.Name;
            drpNumber.DataValueField = BFThirdUnitNumberMetadata.ColumnNames.ThirdUnitNumberID;
            drpNumber.DataBind();

            drpNumber.Items.Insert(0, new ListItem("请选择凭证柜号", ""));

            show = "show";
        }
    }

    #region  Delete_Command
    protected void Delete_Command(object sender, CommandEventArgs e)
    {
        decimal Id = decimal.Parse(e.CommandArgument.ToString());
        
        FinReceiptCollection finReceiptCollection = new FinReceiptCollection();
        finReceiptCollection.Query.Where(finReceiptCollection.Query.BoxID == Id, finReceiptCollection.Query.IsDeleted==false);
        finReceiptCollection.Query.Load();
        if (finReceiptCollection.Count > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showMsg('此凭证盒中存有凭证，不能删除！')", true);
        }
        else
        {
            FinBox finBox = new FinBox();
            finBox.LoadByPrimaryKey(Id);
            finBox.MarkAsDeleted();
            finBox.Save();
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showMsg('删除凭证盒成功！')", true);
        }

    }
    #endregion
}